//--包含你要使用的头文件--//
#include "config.h" //通用配置头文件
/*************  本地常量声明    **************/
/*************  IO口定义    **************/
/*************  本地变量声明    **************/
/*************  本地函数声明    **************/
void Test_Lines(void);          // 测试1: 线条绘制
void Test_Pixels(void);         // 测试2: 像素点
void Test_Fill(void);           // 测试3: 填充效果
void Test_BouncingBall(void);   // 测试4: 弹跳球
void Test_Waveform(void);       // 测试5: 波形效果
void Test_TextDisplay(void);    // 测试6: 文字显示
void Test_NumberDisplay(void);  // 测试7: 数字显示
void Test_FloatDisplay(void);   // 测试8: 浮点数显示
void Test_WordWrap(void);       // 测试9: 自动换行
void Test_Newline(void);       // 测试10: 换行符
void Test_Dashboard(void);      // 测试11: 仪表盘
void Test_Realtime(void);       // 测试12: 实时刷新
void Test_Alignment(void);     // 测试13: 对齐方式
void Test_Sensors(void);       // 测试14: 传感器数据模拟
/****************  外部函数声明和外部变量声明 *****************/
/*******************************************************************************
 * 函 数 名         : main
 * 函数功能		     : 主函数
 * 输    入         : 无
 * 输    出         : 无
 *******************************************************************************/
int main(void)
{
	System_init(); // 天才第一步，使用SysConfig配置文件进行初始化。

#ifdef DEBUG // 调试模式下，检查GPIO是否复位和上电
#include <assert.h>
	assert(DL_GPIO_isReset(LED_PORT));
	assert(DL_GPIO_isPowerEnabled(LED_PORT));
#endif
	set_systick_ms(1); // 设置SysTick中断周期为1ms

	// ========== OLED DMA 缓存模式测试 ==========
	// 初始化（自动配置 DMA，之后硬件会自动刷新）
	OLED_Init();
	delay_ms(500);

	// 主循环：OLED演示
	while (1)
	{
		Test_Lines();
		Test_Pixels();
		Test_Fill();
		Test_BouncingBall();
		Test_Waveform();
		Test_TextDisplay();
		Test_NumberDisplay();
		Test_FloatDisplay();
		Test_WordWrap();
		Test_Newline();
		Test_Dashboard();
		Test_Realtime();
		Test_Alignment();
		Test_Sensors();
	}

	return 0;
}

// ========== 测试函数实现 ==========

// 测试1: 线条绘制
void Test_Lines(void)
{
	OLED_ClearBuffer();
	OLED_DrawHLine(0, 32, 128, 1);
	OLED_DrawVLine(64, 0, 64, 1);
	for (uint8_t i = 0; i < 4; i++)
	{
		OLED_DrawRect(i * 5, i * 5, 128 - i * 10, 64 - i * 10, 1);
	}
	delay_ms(2000);
}

// 测试2: 像素点绘制
void Test_Pixels(void)
{
	OLED_ClearBuffer();
	for (uint8_t x = 0; x < 128; x += 4)
	{
		for (uint8_t y = 0; y < 64; y += 4)
		{
			OLED_DrawPixel(x, y, 1);
		}
	}
	delay_ms(2000);
}

// 测试3: 填充效果
void Test_Fill(void)
{
	OLED_ClearBuffer();
	for (uint8_t i = 0; i < 8; i++)
	{
		OLED_FillRect(i * 16, 0, 16, 64, i % 2);
	}
	delay_ms(2000);
}

// 测试4: 弹跳球动画
void Test_BouncingBall(void)
{
	int16_t x = 64, y = 32;
	int8_t vx = 2, vy = 2;
	for (uint16_t i = 0; i < 200; i++)
	{
		x += vx;
		y += vy;
		if (x <= 0 || x >= 124) vx = -vx;
		if (y <= 0 || y >= 60) vy = -vy;
		OLED_ClearBuffer();
		OLED_DrawRect(0, 0, 128, 64, 1);
		OLED_FillRect(x, y, 4, 4, 1);
		delay_ms(20);
	}
	delay_ms(500);
}

// 测试5: 波形效果
void Test_Waveform(void)
{
	for (uint16_t offset = 0; offset < 128; offset++)
	{
		OLED_ClearBuffer();
		OLED_DrawHLine(0, 32, 128, 1);
		for (uint8_t x = 0; x < 128; x++)
		{
			uint8_t y = 32 + ((x + offset) % 32 < 16 ? 10 : -10);
			if (y < 64) OLED_DrawPixel(x, y, 1);
		}
		delay_ms(30);
	}
	delay_ms(500);
}

// 测试6: 文字显示
void Test_TextDisplay(void)
{
	OLED_ClearBuffer();
	OLED_DrawString(0, 0, "Hello OLED!", 1);
	OLED_DrawString(0, 10, "ASCII Test", 1);
	OLED_DrawString(0, 20, "0123456789", 1);
	OLED_DrawString(0, 30, "ABCDEFGHIJK", 1);
	OLED_DrawString(0, 40, "abcdefghijk", 1);
	OLED_DrawString(0, 50, "!@#$%^&*()", 1);
	delay_ms(3000);
}

// 测试7: 数字显示
void Test_NumberDisplay(void)
{
	OLED_ClearBuffer();
	OLED_DrawString(0, 0, "Number Test:", 1);
	OLED_DrawString(0, 12, "Pos:", 1);
	OLED_DrawNumber(30, 12, 12345, 1);
	OLED_DrawString(0, 24, "Neg:", 1);
	OLED_DrawNumber(30, 24, -9876, 1);
	OLED_DrawString(0, 36, "Zero:", 1);
	OLED_DrawNumber(36, 36, 0, 1);
	OLED_DrawString(0, 48, "Big:", 1);
	OLED_DrawNumber(30, 48, 999999, 1);
	delay_ms(3000);
}

// 测试8: 浮点数显示
void Test_FloatDisplay(void)
{
	OLED_ClearBuffer();
	OLED_DrawString(0, 0, "Float Test:", 1);
	OLED_DrawString(0, 12, "Pi:", 1);
	OLED_DrawFloat(20, 12, 3.14159, 3, 1);
	OLED_DrawString(0, 24, "E:", 1);
	OLED_DrawFloat(14, 24, 2.71828, 4, 1);
	OLED_DrawString(0, 36, "Temp:", 1);
	OLED_DrawFloat(36, 36, 25.5, 1, 1);
	OLED_DrawString(60, 36, "C", 1);
	OLED_DrawString(0, 48, "Volt:", 1);
	OLED_DrawFloat(36, 48, 3.3, 2, 1);
	OLED_DrawString(60, 48, "V", 1);
	delay_ms(3000);
}

// 测试9: 自动换行显示
void Test_WordWrap(void)
{
	OLED_ClearBuffer();
	OLED_DrawString(0, 0, "Word Wrap Test:", 1);
	OLED_DrawString(0, 12, "This is a very long string that will wrap automatically.", 1);
	delay_ms(3000);
}

// 测试10: 换行符显示
void Test_Newline(void)
{
	OLED_ClearBuffer();
	OLED_DrawString(0, 0, "Newline Test:\nLine 1\nLine 2\nLine 3", 1);
	delay_ms(3000);
}

// 测试11: 混合显示（文字+图形）
void Test_Dashboard(void)
{
	OLED_ClearBuffer();
	OLED_DrawString(25, 0, "Dashboard", 1);
	OLED_DrawRect(0, 10, 128, 54, 1);
	OLED_DrawString(5, 15, "CPU:", 1);
	OLED_DrawNumber(35, 15, 75, 1);
	OLED_DrawString(55, 15, "%", 1);
	OLED_DrawRect(70, 15, 50, 8, 1);
	OLED_FillRect(72, 17, 37, 4, 1);
	OLED_DrawString(5, 28, "RAM:", 1);
	OLED_DrawNumber(35, 28, 45, 1);
	OLED_DrawString(55, 28, "%", 1);
	OLED_DrawRect(70, 28, 50, 8, 1);
	OLED_FillRect(72, 30, 22, 4, 1);
	OLED_DrawString(5, 45, "Temp:", 1);
	OLED_DrawFloat(35, 45, 42.3, 1, 1);
	OLED_DrawString(65, 45, "C", 1);
	delay_ms(3000);
}

// 测试12: 实时数据测试
void Test_Realtime(void)
{
	for (uint16_t i = 0; i < 100; i++)
	{
		OLED_ClearBuffer();
		OLED_DrawString(20, 0, "Real-time", 1);
		OLED_DrawHLine(0, 9, 128, 1);
		OLED_DrawString(0, 12, "Count:", 1);
		OLED_DrawNumber(42, 12, i, 1);
		float voltage = 3.0 + (i % 20) * 0.1;
		OLED_DrawString(0, 24, "Volt:", 1);
		OLED_DrawFloat(36, 24, voltage, 2, 1);
		OLED_DrawString(72, 24, "V", 1);
		OLED_DrawRect(0, 40, 128, 12, 1);
		uint8_t progress = i * 128 / 100;
		OLED_FillRect(2, 42, progress - 4, 8, 1);
		OLED_DrawNumber(50, 55, i, 1);
		OLED_DrawString(70, 55, "%", 1);
		delay_ms(50);
	}
	delay_ms(1000);
}

// 测试13: 文字对齐
void Test_Alignment(void)
{
	OLED_ClearBuffer();
	OLED_DrawString(0, 0, "Align Test", 1);
	OLED_DrawString(0, 12, "Left", 1);
	OLED_DrawString((128 - 6 * 6) / 2, 24, "Center", 1);
	OLED_DrawString(128 - 5 * 6, 36, "Right", 1);
	delay_ms(3000);
}

// 测试14: 传感器数据模拟显示
void Test_Sensors(void)
{
	for (uint8_t i = 0; i < 30; i++)
	{
		OLED_ClearBuffer();
		OLED_FillRect(0, 0, 128, 12, 1);
		OLED_DrawString(20, 2, "Sensors", 0);
		float temp = 20.0 + (i % 15);
		OLED_DrawString(5, 15, "Temp:", 1);
		OLED_DrawFloat(42, 15, temp, 1, 1);
		OLED_DrawString(72, 15, "C", 1);
		uint8_t humidity = 40 + (i % 30);
		OLED_DrawString(5, 27, "Humi:", 1);
		OLED_DrawNumber(42, 27, humidity, 1);
		OLED_DrawString(72, 27, "%", 1);
		int32_t pressure = 101300 + (i * 100);
		OLED_DrawString(5, 39, "Pres:", 1);
		OLED_DrawNumber(42, 39, pressure, 1);
		OLED_DrawString(5, 51, "Status: ", 1);
		if (i % 3 == 0) OLED_DrawString(60, 51, "OK", 1);
		else if (i % 3 == 1) OLED_DrawString(60, 51, "WARN", 1);
		else OLED_DrawString(60, 51, "ERR", 1);
		delay_ms(200);
	}
}
